<?php
global $_W, $_GPC;
$uid = $_GPC['uid'];
if (empty($uid)) {
    return $this->getresult(1, '请先登录', array('code' => 3001, 'msg' => '请先登录'));
}
/***校验token开始***/
if ($uid) {
    $token = $_GPC['token'];
    if (empty($token)) {
        return $this->getresult(1, '请重新登录', ['code' => 4001, 'msg' => '请重新登录']);
    }
    $token_result = $this->checktoken($uid, $token);
    if ($token_result == 1002) {
        return $this->getresult(1, '请重新登录', ['code' => 4002, 'msg' => '请重新登录']);
    } elseif ($token_result == 1003) {
        return $this->getresult(1, '请重新登录', ['code' => 4003, 'msg' => '请重新登录']);
    } elseif ($token_result == 1001) {
        //  更新token
        pdo_update($this->t_user_token, array('createtime' => time()), array('uid' => $uid, 'token' => $token, 'weid' => $_W['uniacid']));
    }
}
/***校验token结束***/
$weid = $_W['uniacid'];
//获取已报名的课程
$contion = 's.weid = :weid AND s.uid = :uid';
$params = [':weid' => $weid, ':uid' => $uid];
$lessonList = pdo_fetchall("select s.id,s.uid,sj.name as subjectname,l.title as ltitle,l.id as lid from " . tablename($this->t_signup) . " s LEFT JOIN " . tablename($this->t_lesson) . " l ON s.lessonid = l.id LEFT JOIN " . tablename($this->t_subject) . " sj ON l.subjectid = sj.id where " . $contion, $params);

//线下课
$phone = pdo_fetchcolumn("select phone from " . tablename('goouc_class_user') . "  where id = :uid", array(':uid' => $uid));
$lparams = [':weid' => $weid, ':phone' => $phone, ':is_underline' => 1];
$underlineList = pdo_fetchall("select sj.name as subjectname,l.title as ltitle,l.id as lid from " . tablename('goouc_class_lesson_underline_student') . " us LEFT JOIN " . tablename('goouc_class_lesson') . " l ON us.lesson_id = l.id LEFT JOIN " . tablename('goouc_class_subject') . " sj ON l.subjectid = sj.id where us.weid = :weid AND us.phone = :phone AND l.is_underline = :is_underline", $lparams);
$lessonList = array_merge($lessonList, $underlineList);
//把存在习题的课程放在一个数组里
$practiceLesson = [];
foreach ($lessonList as $ks => $vs) {
    $pnum = pdo_fetchall("select p.id from " . tablename('goouc_class_practice') . " p LEFT JOIN " . tablename('goouc_class_son') . " s ON p.sonid = s.id WHERE s.weid = :weid AND s.status = :status AND s.lessonid = :lid ", array(':weid' => $weid, ':status' => 1, ':lid' => $vs['lid']));
    if (count($pnum) > 0) {
        $practiceLesson[] = $vs;
    }
}
foreach ($practiceLesson as &$v) {
    //章节总数量
    $tsql = "SELECT COUNT(*) FROM " . tablename('goouc_class_son') . " WHERE weid = :weid AND lessonid = :lessonid AND status = :status";
    $chaptertotal = pdo_fetchcolumn($tsql, array(':weid' => $weid, ':lessonid' => $v['lid'], ':status' => 1));
    //课程总星星数量
    $v['totalStar'] = 3 * $chaptertotal;
    //会员该课程获取到的星星数量
    $userstar = pdo_fetch("select sum(num) as totalnum from " . tablename('goouc_class_user_star') . " WHERE weid = :weid AND lessonid = :lessonid AND uid = :uid AND status = :status", array(':weid' => $weid, ':uid' => $uid, ':lessonid' => $v['lid'], ':status' => 1));
    $v['user_star'] = empty($userstar['totalnum']) ? 0 : $userstar['totalnum'];
    //学习进度条
    $studySon = pdo_fetchcolumn("select COUNT(*) as studySon FROM " . tablename('goouc_class_stady') . " cs LEFT JOIN " . tablename('goouc_class_son') . " s ON cs.sonid = s.id LEFT JOIN " . tablename('goouc_class_lesson') . " l ON s.lessonid = l.id where cs.weid = :weid AND cs.uid = :uid AND l.id = :lid AND s.status = :status", array(':weid' => $weid, ':uid' => $uid, ':lid' => $v['lid'], ':status' => 1));
    if ($chaptertotal) {
        $jindu = empty($chaptertotal) ? 0 : ($studySon / $chaptertotal);
        $v['jindu'] = floor($jindu * 100) / 100;
    } else {
        $v['jindu'] = 0;
    }
    //该课程学习状态
    $studyStatus = pdo_fetch("select id FROM " . tablename('goouc_class_user_study_speed') . " WHERE weid = :weid AND lessonid = :lessonid AND uid = :uid limit 1", array(':weid' => $weid, ':lessonid' => $v['lid'], ':uid' => $uid));
    $v['studyStatus'] = empty($studyStatus['id']) ? 2 : 1;//1:已学习 2:未学习
}
return $this->getresult(0, '习题课程数据', ['code' => 200, 'practiceLesson' => $practiceLesson]);
